package gybase01

/**
 ********************************************************
 * Author: chinadragon
 * Time: 2021/11/16 下午10:21
 * Name:
 * Overview:
 * Usage:
 ********************************************************
 */
class Car {
    //var val

    def miles

//    final def year = 2021
    final year = 2021//省略略 def 注意 这里的 final 代表只读读属性，和val不一样
    private year2 = 2021//省略略 def 注意 这里的 final 代表只读读属性，和val不一样

}

//def car1 = new NewCar(miles: 1000,year: 2022)//这里读year不能被修改
//报异常
//groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: year for class: test.NewCar
//	at test.TestGroovy01.run(TestGroovy01.groovy:22)
def car = new Car(miles: 1000)
println car.miles

def name = 'carname'
def  realname = "$name:AAAA"//使用插值表达式
def  realnam2 = "AAAA"

def str3 ='''

carName
is
$name

'''

/*
    单引号和双引号的区别

    单引号是存粹的字符串
    双引号是可以进行插值的
 */

println name.getClass()
println realname.getClass()
println realnam2.getClass()
//class java.lang.String
//class org.codehaus.groovy.runtime.GStringImpl
//class java.lang.String

println  str3
//carName
//is
//$name

def array = [1,2,3]
println array.getClass()
//class java.util.ArrayList

def array2 = [1,2,3] as int[]
println array2.getClass()
//class [I

int[] array3 = [1,2,3]
println array3.getClass()
//class [I

def array4 = [1,"2",3]
println array4.getClass()
println array4[1]
println array4[1].getClass()
//class java.util.ArrayList
//2
//class java.lang.String

println"--------------------------------------------------------------------------------------------------------"

String[] array5 = ["1",new Object(),"3"]
println array5.getClass()
println array5[1].getClass()
println array5[1]//这里调用的tostring
println array5[1].toString()
//class [Ljava.lang.String;
//class java.lang.String
//java.lang.Object@45673f68
//java.lang.Object@69e308c6

(0..10).forEach{//包含10
    print it+" "
}
//0 1 2 3 4 5 6 7 8 9 10

println "动态性 --------------------------------------------------------------"


(0..<10).forEach{//不包含10
    print it+" "
}
//0 1 2 3 4 5 6 7 8 9

println "动态性 --------------------------------------------------------------"

Integer.metaClass.add1000{
    delegate + 1000
}

println 666.add1000()
//1666

def range = 0 .. 10

println range.getClass()

//0 1 2 3 4 5 6 7 8 9 10 动态性 --------------------------------------------------------------
//0 1 2 3 4 5 6 7 8 9 动态性 --------------------------------------------------------------
//1666
//class groovy.lang.IntRange

// kotlin 借鉴 groovy很多特性

def map = [a:11,b:12]//String, Integer
map.forEach{
    k,v->
        println "key = ${k}  k 类型： ${k.getClass()} , value = $v"
}
//key = a  k 类型： class java.lang.String , value = 11
//key = b  k 类型： class java.lang.String , value = 12

println map['a']
//11

//空映射 [:]
def emptyMap = [:]
println emptyMap.size()
//0








